package com.minimalist.tc.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.config.easyexcel.ExcelDataListener;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.tc.entity.excel.TcOrderExcelData;
import com.minimalist.common.entity.tc.entity.vo.order.TcOrderQueryVO;
import com.minimalist.common.entity.tc.entity.vo.order.TcOrderVO;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.Update;
import com.minimalist.tc.service.TcOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "停车_订单管理")
@RequestMapping("/tc/order")
public class TcOrderController{
    private static final Logger log = LoggerFactory.getLogger(TcOrderController.class);


    @Autowired
    private TcOrderService service;

    @PostMapping("/add")
    @SaCheckPermission("tc:order:add")
    @Operation(summary = "添加订单")
    public ResponseEntity<Void> add(
            @RequestBody @Validated(Add.class) TcOrderVO orderVO){
        service.addOrder(orderVO);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/delete")
    @SaCheckPermission("tc:order:delete")
    @Operation(summary = "删除订单 -> 根据订单ID删除")
    public ResponseEntity<Void> delete(
            @RequestParam("orderId") @NotNull(message = "订单ID不能为空") @Parameter(name = "orderId", required = true, description = "订单ID") Long orderId){
        service.deleteOrderByOrderId(orderId);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/update")
    @SaCheckPermission("tc:order:update")
    @Operation(summary = "修改订单")
    public ResponseEntity<Void> update(
            @RequestBody @Validated(Update.class) TcOrderVO orderVO){
        service.updateOrderByOrderId(orderVO);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/getPageList")
    @SaCheckPermission("tc:order:get")
    @Operation(summary = "查询订单列表(分页)")
    public ResponseEntity<PageResp<TcOrderVO>> getPageList(TcOrderQueryVO queryVO){
        return ResponseEntity.ok(service.getPageOrderList(queryVO));
    }

    @GetMapping("/get/{orderId}")
    @SaCheckPermission("tc:order:get")
    @Operation(summary = "根据订单ID查询订单")
    public ResponseEntity<TcOrderVO> get(
            @PathVariable(value = "orderId") @NotNull(message = "订单ID不能为空") @Parameter(name = "orderId", description = "参数ID", required = true) Long orderId){
        return ResponseEntity.ok(service.getOrderByOrderId(orderId));
    }

    @PostMapping("/importExcel")
    @SaCheckPermission("tc:order:add")
    @Operation(summary = "Excel导入订单数据")
    public ResponseEntity<Map<String, Object>> importExcel(
            @RequestParam("file") MultipartFile file) throws IOException{

        if (file.isEmpty()){
            throw new BusinessException("上传文件不能为空");
        }

        // 文件类型校验
        String originalFilename = file.getOriginalFilename();
        if (originalFilename == null || (!originalFilename.endsWith(".xlsx") && !originalFilename.endsWith(".xls"))){
            throw new BusinessException("请上传Excel文件(.xlsx或.xls)");
        }

        int batchSize = 1000;
        // 创建Excel数据监听器
        ExcelDataListener<TcOrderExcelData> listener = new ExcelDataListener<>
                (batchSize, dataList -> service
                        .batchAddOrders(dataList, batchSize));

        // 读取Excel文件
        EasyExcel.read(file.getInputStream(), TcOrderExcelData.class, listener)
                .sheet().doRead();

        // 返回导入结果
        Map<String, Object> result = new HashMap<>(2);
        result.put("success", true);
        result.put("total", listener.getTotalCount());

        return ResponseEntity.ok(result);
    }
}
